package app.action;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import javazoom.jl.decoder.JavaLayerException;

import app.ui.AbsCommonPanel;
import app.ui.MP3PlayerUi;

public class MP3PlayerAction extends AbsCommonPanelAction implements Runnable {
	private MP3PlayerUi mPanel;

	private File file;
	private AdvancedPlayer player;
	private Thread t;

	private final int PLAYING = 1, STOP = 2, PAUSE = 3;
	private int playStatus = 0;
	private int paramInt1 = 0; // start play position
	private int paramInt2 = Integer.MAX_VALUE; // stop play position

	public MP3PlayerAction(AbsCommonPanel absCommonPanel) {
		super(absCommonPanel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String[] btnTitles = mPanel.getBtnTexts();
		JButton jButton = (JButton) e.getSource();

		int index = 0;
		for (; index < btnTitles.length; index++) {
			if (btnTitles[index].equals(jButton.getText())) {
				break;
			}
		}

		switch (index) {
		case 0: // Play button
			doPlayClick();
			break;
		case 1: // Stop button
			doStopClick();
			break;
		case 2: // Browse button
			doBrowseClick();
			break;
		default: // Pause button
			doPauseClick();
			break;
		}

		// Close button
		if (jButton == mPanel.getCloseBtn()) {
			doStopClick();
			System.exit(0);
		}
	}

	private void doPauseClick() {
		if (playStatus == PLAYING) {
			playStatus = PAUSE;

			if (player != null) {
				paramInt1 = player.getPosition();
			}
			stopPlayer();
		}
	}

	private void doPlayClick() {
		// stop playing if there is.
		if (playStatus == PLAYING) {
			mPanel.getjButtons()[1].doClick(); // click stop button
		}

		mPanel.getjButtons()[0].setText("Pause");
		mPanel.getjButtons()[0].setIcon(new ImageIcon(getClass().getResource("/Icons/pause.png")));

		// configure Player
		doConfigurePlayer();

		// start player
		if (t == null) {
			this.t = new Thread(this);
			t.setPriority(Thread.MAX_PRIORITY);
			t.start();
		}
	}

	private void doStopClick() {
		stopPlayer();
		playStatus = STOP;
	}

	private void stopPlayer() {
		if (player != null) {
			player.stop();
		}
	}

	private void doBrowseClick() {
		String[] extensionOfFile = { "mp3" };
		FileUtilities fileUtilities = new FileUtilities(extensionOfFile, "MP3");

		JFileChooser jFileChooser = new JFileChooser("C:/Users/mvv/Downloads/Music");
		jFileChooser.setFileFilter(fileUtilities);

		int iVal = jFileChooser.showOpenDialog(mPanel);

		if (iVal == JFileChooser.APPROVE_OPTION) {
			this.file = jFileChooser.getSelectedFile();

			// display location
			mPanel.getjLabel().setText(file.getPath());

			// set button enable
			mPanel.getjButtons()[0].setEnabled(true);
			mPanel.getjButtons()[1].setEnabled(true);

			// click play button
			doPlayClick();

			// focus on play button
			mPanel.getjButtons()[0].requestFocus();
		}
	}

	private void doConfigurePlayer() {
		if (player != null) {
			return;
		}

		try {
			this.player = new AdvancedPlayer(new FileInputStream(file));
			this.player.setPlayBackListener(new MyPlayerAction());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doCatenation() {
		this.mPanel = (MP3PlayerUi) absCommonPanel;
	}

	@Override
	public void run() {
		if (player != null) {
			try {
				if (playStatus == PAUSE) {
					player.play(paramInt1, paramInt2);
				} else {
					player.play();
				}
			} catch (JavaLayerException e) {
				e.printStackTrace();
			}
		}
	}

	class MyPlayerAction extends PlaybackListener {

		@Override
		public void playbackStarted(PlaybackEvent evt) {
			playStatus = PLAYING;
		}

		@Override
		public void playbackFinished(PlaybackEvent evt) {
			if (playStatus == PLAYING) {
				playStatus = STOP;
				paramInt1 = 0;
			}

			if (t != null) {
				if (player != null) {
					player = null;
				}
				t = null;
			}

			mPanel.getjButtons()[0].setText("Play");
			mPanel.getjButtons()[0].setIcon(new ImageIcon(getClass().getResource("/Icons/play.png")));
		}

	}
}
